package de.htwds.mada.todolistDB;

import java.sql.SQLException;
import java.util.List;
import java.util.ListIterator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import de.htwds.mada.R;
import de.htwds.mada.todolistDB.model.Priority;

public class TODODBNewPriorityActivity extends Activity {

	//------------------------- Felder ------------------------------------------//
	private MadaArrayAdapter<Priority> listAdapter;
	private EditText eingabePriority;
	private ListView liste;
	private Priority selectedPriority = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.tododb_new_priority);
		
		//wenn noetig Adapter initialisieren
		
		liste = (ListView) findViewById(R.id.lvwPriorities);
		
		if(listAdapter == null)
			listAdapter = new MadaArrayAdapter<Priority>(this);
		
		liste.setAdapter(listAdapter);
				
		dbZugriff(); //Datenbankzugriff lesen
		
		eingabePriority = (EditText) findViewById(R.id.txtPriority);
		Button save 	= (Button) findViewById(R.id.btnSavePriority);
		Button delete 	= (Button) findViewById(R.id.btnDeletePriority);
		
		save.setOnClickListener(new OnClickListener() {
			@Override			
			public void onClick(View v) {
				String eingabe = eingabePriority.getText().toString().trim();
				if(eingabe != null){
					Priority prio = new Priority();
					prio.setName(eingabe);
					try {
						TODODBListActivity.db.createOrUpdatePriority(prio);
						eingabePriority.setText("");
						Toast.makeText(TODODBNewPriorityActivity.this, R.string.db_entry_created, Toast.LENGTH_SHORT).show();
					} catch (SQLException e) {
						Toast.makeText(TODODBNewPriorityActivity.this, R.string.error_db_write, Toast.LENGTH_SHORT).show();
						e.printStackTrace();
					}
					
					dbZugriff(); //adapter aktualisieren
				} else {
					Toast.makeText(TODODBNewPriorityActivity.this, R.string.todo_db_error_empty, Toast.LENGTH_SHORT).show();
				}
				
			}
		});
		
		delete.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				if(selectedPriority != null){
					try {
						TODODBListActivity.db.deletePriority(selectedPriority);
						eingabePriority.setText("");
						Toast.makeText(TODODBNewPriorityActivity.this, R.string.db_entry_deleted, Toast.LENGTH_SHORT).show();
					} catch (SQLException e) {
						e.printStackTrace();
						Toast.makeText(TODODBNewPriorityActivity.this, R.string.error_db_write, Toast.LENGTH_SHORT).show();
					}
					
					dbZugriff(); //adapter aktualisieren
				} else {
					Toast.makeText(TODODBNewPriorityActivity.this, R.string.todo_db_error_not_found, Toast.LENGTH_SHORT).show();
				}
				
			}
		});
		
		liste.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				// TODO Auto-generated method stub
				selectedPriority = (Priority) parent.getAdapter().getItem(position);
				eingabePriority.setText(selectedPriority.getName());
				
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		dbZugriff();
		
	}
	
	/**
	 * Methode, die den Zugriff auf die Datenbank veranlasst
	 */
	private void dbZugriff(){
		listAdapter.clear();
		
		List<Priority> list = null;
		try {
			list = TODODBListActivity.db.getPriorities(); //Datenbank-Zugriff lesen
		} catch (SQLException e) {
			e.printStackTrace();
			Toast.makeText(TODODBNewPriorityActivity.this, R.string.error_db_write, Toast.LENGTH_SHORT).show(); //Toast wenn es schief geht
		}
		
		//Ergebnisse werden durchiteriert und in die Liste eingetragen
		ListIterator<Priority> iterator = list.listIterator();
		while(iterator.hasNext()){
			Priority prio = iterator.next();
			listAdapter.add(prio);
		}
		
		listAdapter.notifyDataSetChanged();
	}
	
	
}
